<template>
  <div class="dashboard-editor-container" :style="{backgroundImage: 'url(' + bgImg + ')' }">
    <div class="login-container">
      <div class="filter-container">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="off" label-position="left"
                 label-width="100px">
          <el-form-item label="用户名" prop="userName">
            <el-input type="text" v-model="loginForm.userName" name="username" placeholder="4-20位字符，仅支持字母、数字,'-','_'及组合"
                      style="width: 55%;border-radius: 5px;border: 1px #ccc;"/>
          </el-form-item>
          <el-form-item label="登录密码" prop="password">
            <el-input :type="passwordType" v-model="loginForm.password" name="password" auto-complete="new-password" placeholder="登录密码"
                      style="width: 55%;border-radius: 5px;border: 1px #ccc;"/>
            <span class="show-pwd" @click="showPwd">
            <svg-icon icon-class="eye" />
          </span>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPwd">
            <el-input :type="passwordType" v-model="loginForm.confirmPwd" name="confirmPwd" placeholder="请再次输入确认密码"
                      style="width: 55%;border-radius: 5px;border: 1px #ccc;"/>
            <!--<span class="show-pwd" @click="showPwd2">
					<svg-icon icon-class="eye" />
				  </span>-->
          </el-form-item>
          <el-form-item label="联系人手机" prop="phone">
            <el-input v-model="loginForm.phone" auto-complete="no" placeholder="请输入联系人手机号码" style="width: 55%;border-radius: 5px;border: 1px #ccc;"/>
          </el-form-item>
          <el-form-item label="图片验证码" prop="checkpic">
            <el-input v-model="loginForm.checkpic" auto-complete="no" name="checkpic" placeholder="请输入4位图片验证码" style="width:30%;border-radius: 5px;border: 1px #ccc;"/>
            <span class="show-pwd" @click="changePic" style="right: 50%;">
            <img :src="checkpicSRC" style="display: inline-block;width: 145%;height: 38px;">
          </span>
          </el-form-item>
          <el-form-item label="短信验证码" prop="checkSms">
            <el-input v-model="loginForm.checkSms" auto-complete="no" name="checkpic" placeholder="请输入短信验证码" style="width:30%;border-radius: 5px;border: 1px #ccc;"/>
            <el-button :loading="phoneloading" type="primary" :disabled="phoneDisabled" style="width: 130px;right: 45%;" class="show-pwd"
                       @click.stop="handlesend">{{ phoneText }}
            </el-button>
            <!--<el-button :loading="phoneloading" type="primary" :disabled="phoneDisabled" style="width: 130px" @click.stop="handlesend" class="show-pwd">
			  {{ phoneText }}
			</el-button>-->
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="register_ed" @click.native.prevent="handleLogin">立即注册</el-button>
            <span class="accounts_log">
            已有账号？去 <a @click="login">登录</a>
          </span>
          </el-form-item>
        </el-form>
      </div>

    </div>
    <footercopy/>
  </div>
</template>

<script>
import { doGet } from '@/api/index'
import bgImg from '../../assets/xh-bg.jpg'
import Footercopy from '../company/footercopy'

export default {
  name: 'ResetPassword',
  components: {
    Footercopy,
  },
  data() {
    const validatePassword = (rule, value, callback) => {
      let val = /^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$).{8,20}$/;
      if (!val.test(value)) {
        callback(new Error('8-20位字符，包含：大、小写字母、数字、符号（除空格）至少两种'))
      } else {
        callback()
      }
    }
    const validatePassword2 = (rule, value, callback) => {
      if (value !== this.loginForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    const validateuserName = (rule, value, callback) => {
      let val = /^([a-zA-Z0-9\.\@\!\#\$\%\^\&\_\-\*\(\)]){4,20}$/i;
      if (!val.test(value)) {
        callback(new Error('4-20位字符，仅支持字母、数字、"-"、"_"及组合'))
      } else {
        callback()
      }
    }
    const validateCheckSms = (rule, value, callback) => {
      if (value.length != 6) {
        callback(new Error('短信验证码必须为6位'))
      } else {
        callback()
      }
    }
    let mobileValidate = (rule, value, callback) => {
      let reg = /^[1][0-9]{10}$/;
      if (!reg.test(value)) {
        callback(new Error('手机号格式不正确'));
      } else {
        callback()
      }
    }
    return {
      bgImg: bgImg,
      checkpicSRC: '',
      loginForm: {
        userName: '',
        password: '',
        confirmPwd: '',
        checkSms: '',
        phone: '',
        checkpic: '',
        extendCode: ''
      },
      loginRules: {
        userName: [{ required: true, message: '4-20位字符，仅支持字母、数字、"-"、"_"及组合', trigger: 'blur' },
          { validator: validateuserName, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '8-20位字符，包含：大、小写字母、数字、符号（除空格）至少两种', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ],
        confirmPwd: [
          { required: true, message: ' 二次密码不能为空', trigger: 'blur' },
          { validator: validatePassword2, trigger: 'blur' }
        ],
        checkSms: [
          { required: true, message: '手机验证码错误请重新输入', trigger: 'blur' },
          { validator: validateCheckSms, trigger: 'blur' }
        ],
        phone: [{ required: true, message: '请输入正确手机号码,只能包含数字', trigger: 'blur' },
          { validator: mobileValidate, trigger: 'blur' }],
        checkpic: [{ required: true, message: '图片验证码错误请重新输入', trigger: 'blur' }],
      },
      phoneloading: false,
      passwordType: 'password',
      passwordTypeTwo: 'password2',
      loading: false,
      phoneDisabled: false,
      timer: null,
      phoneText: '发送验证码'
    }
  },
  watch: {
    loginForm: {
      handler: function(val) {
        let reg = /^[1][0-9]{10}$/;
        if (!reg.test(val.phone)) {
          this.phoneDisabled = true
        } else {
          if (!this.timer) {
            this.phoneDisabled = false
          }
        }
      },
      deep: true
    }
  },
  created() {
    this.changePic()
    console.log('6666: ' + this.$route.query.extendCode)
  },
  destroyed() {
    clearTimeout(this.timer)
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },

    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid && !this.loading) {
          this.loading = true
          this.loginForm.extendCode = this.$route.query.extendCode
          doGet('regist.html?op=regist', this.loginForm).then((response) => {
            this.loading = false
            this.$router.push({ path: '/login/index' })
            localStorage.removeItem('extendCode')
            this.$notify.success({
              title: '成功',
              message: '注册成功'
            })
          }).catch((error) => {
            this.$notify.error({
              title: '失败',
              message: error.message
            })
            this.loading = false
          })
        } else {
          return false
        }
      })
    },
    changePic() {
      doGet('checkpic.html?name=regist', '').then((response) => {
        this.checkpicSRC = 'data:image/png;base64,' + response
        console.log('susccess' + response)
      }).catch((error) => {
        this.checkpicSRC = 'data:image/png;base64,' + error
      })
    },

    handlesend() {
      this.phoneloading = true
      doGet('sendsms.html', { phone: this.loginForm.phone }).then((response) => {
        this.phoneDisabled = true
        this.phoneloading = false
        this.setTime(300)
      }).catch((error) => {
        this.$notify.error({
          title: '失败',
          message: error.message
        })
        this.phoneloading = false
      })
    },

    setTime(time) {
      if (time <= 0) {
        this.phoneText = '重新获取'
        this.phoneDisabled = false
        clearTimeout(this.timer)
        return
      }
      this.phoneText = `还剩${time}s`
      this.timer = setTimeout(() => {
        time--
        this.setTime(time)
      }, 1000)
    },
    login() {
      this.$router.push({ path: '/index'})
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $bg:#2d3a4b;
  $light_gray:#eee;

  /* reset element-ui css */
  .login-container {
    overflow: auto;
    width: 100%;

    .el-input {
      display: inline-block;
      input {
        width: 52%;
        background: transparent;
        -webkit-appearance: none;
        border-radius: 5px;
        padding: 12px 5px 12px 15px;
        border: 1px solid #ccc;
        /*color: $light_gray;*/
        color:#333;
        height: 40px;
        /*&:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: #fff !important;
        }*/
      }
    }
  }
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
  $bg:#2d3a4b;
  $dark_gray:#889aa4;
  $light_gray:#eee;

  .dashboard-editor-container {
    padding: 108px 0px 0px;
    background-color: #f7f7f7;
    background-repeat: no-repeat;
    background-size: cover;
    background-attachment: fixed;

    .chart-wrapper {
      background: #fff;
      padding: 16px 16px 0;
      margin-bottom: 32px;
    }
  }
  .login-container {
    /*position: fixed;*/
    height: 100%;
    width: 100%;
    min-width: 1200px;
    max-width: 1920px;
    margin: 0 auto;
    padding-top: 80px;
    .el-form-item {
      background: #fff !important;
      border: 0;
    }
    .login-form {
     /* position: absolute;*/
      left: 0;
      right: 0;
      width: 850px;
      padding: 35px 35px 0 150px;
      /*margin: 120px auto;*/
    }
    .tips {
      font-size: 14px;
      color: #333;
      margin-bottom: 10px;
      span {
    	  &:first-of-type {
    	    margin-right: 16px;
    	  }
      }
    }
.svg-container {
  padding: 6px 5px 6px 15px;
  /*color: $dark_gray;*/
      vertical-align: middle;
      width: 30px;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }
    .title-container {
      position: relative;
      .title {
        font-size: 26px;
        font-weight: 400;
        /*color: $light_gray;*/
        color: #333;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }
    }
    .show-pwd ,.show-forget {
      position: absolute;
      right: 50%;
      top: 1px;
      font-size: 16px;
      /*color: $dark_gray;*/
      cursor: pointer;
      user-select: none;
    }
    .show-forget {
      &:hover {
        color: #333;
        border-bottom: 1px solid #FFFFFF;
      }
    }
    .register_ed {
      width: 170px;
      height: 50px;
      font-size: 18px;
    }
    .accounts_log {
      display: inline-block;
      padding: 7px 38px;
      font-size: 14px;
      a {
        &:hover {
          color: #409EFF;
          border-bottom: 1px solid #409EFF;
        }
      }
    }
  }
</style>
